//光配连接
var ODFConnectChart = {
    DeviceSpace:200,//设备间隔
    PortVerSpace:5,
    PortBodyBackground:'#427d78',
    parseFrom: function(unit_id, slotName, portInfo,cubicleConnection,unitConnection){
        /*
        portInfo:{
            "count": 12,
            "list": [
               {
                    "BOARD_ID": 2,
                    "DIRECTION": "RT",
                    "NAME": "A",
                    "PLUG": "ST",
                    "PORT_ID": 11
                },
                ...
            ]
        }，
        unitConnection：{
            "count":24,
            "list":[
                {
                    "CONN_NAME": "TX001", //光纤名
                    "ID": 11,
                    "NAME": "A",    //光配端口名
                    "PORT2_DIRECTION": "Rx",
                    "PORT2_ID": 7,
                    "PORT2_NAME": "9X-D",
                    "PORT2_PLUG": "LC",
                    "UNIT2_CLASS": "IED",
                    "UNIT2_DESC": "站域保护装置",
                    "UNIT2_ID": 1
                },
                ...
            ]
        },
        cubicleConnection = {
            "count": 12,
            "list": [
                {
                    "CABLE_ID": 107,
                    "CABLE_NAME": "GY-GL100A", //光缆名称
                    "COLOR": 1,//纤芯颜色
                    "CONN_NAME": "1",//纤芯编号
                    "CUBICLE2_DESC": "220kV 1M母线设备GIS智能控制柜",//橱柜名称
                    "CUBICLE2_ID": 19,//橱柜ID
                    "ID": 11,
                    "NAME": "A", //光配端口
                    "PORT2_DIRECTION": "RT",
                    "PORT2_ID": 1611,
                    "PORT2_NAME": "B-I",
                    "PORT2_PLUG": "ST",
                    "TYPE": "GL",//光缆类型，GL,WL,不同颜色
                    "UNIT2_CLASS": "ODF",
                    "UNIT2_DESC": "ODF-A",
                    "UNIT2_ID": 77
                },
                ...
            ]
        }
        */
        Device.bodyBackground = '#6296c3';
        var chart = {};
        chart.x=0;
        chart.y=0;
        chart.slotName = slotName;
        chart.portInfoData = portInfo;
        chart.cubicleConnectionData = cubicleConnection;
        chart.unitConnectionData = unitConnection;
        chart.unit_id = unit_id;//主装置号
        chart.clickedObjects = [];//保存可点击对象
        chart.device = null;//保存自身
        //1.初始化端口信息
        chart.device = Device.createNew(chart.unit_id, '#未知');
        chart.device.data = {};
        chart.device.data.UNIT_CLASS = 'ODF';
        chart.device.ports = [];
        //记录哪些装置已经找到了对应的端口
        var dicUnitHasPort = {};
        var dicCubiclePort = {}; 
        for(var k in chart.portInfoData.list){
            var data = chart.portInfoData.list[k];
            if(data.SLOT != slotName){
                continue;
            }
            var port = PortODF.createNew(data.PORT_ID,data.NAME);
            port.data = data;
            chart.device.ports.push(port);
            //查找与此端口关联的装置数据
            port.deviceConnectData = null;
            for(var kk in chart.unitConnectionData.list){
                if(dicUnitHasPort.hasOwnProperty(kk)){
                    continue;
                }
                var deviceData = chart.unitConnectionData.list[kk];
                if(deviceData.NAME == port.name){
                    port.deviceConnectData = deviceData;
                    dicUnitHasPort[kk] = true;
                    break;
                }
            }
            //查找与此端口关联的橱柜数据
            port.cubicleConnectData = null;
            for(var kk in chart.cubicleConnectionData.list){
                if(dicCubiclePort.hasOwnProperty(kk)){
                    continue;
                }
                var cubicleData = chart.cubicleConnectionData.list[kk];
                if(cubicleData.NAME == port.name){
                    port.cubicleConnectData = cubicleData;
                    dicCubiclePort[kk] = true;
                    break;
                }
            }
        }
        chart.device.ports.sort(ODFConnectChart.PortAscSort);
        chart.device.width = PortODF.width;
        //初始化装置数据，看一个装置连续连接了几个端口
        var prePort = null;
        for(var i=0;i < chart.device.ports.length;i++){
            var port = chart.device.ports[i];
            if(port.deviceConnectData == null){
                prePort = null;
                continue;
            }
            if(prePort != null && port.deviceConnectData.UNIT2_ID == prePort.deviceConnectData.UNIT2_ID){
                port.deviceConnectData.needDraw = false;
                prePort.deviceConnectData.odfPortEndIndex = i;
                continue;
            }
            port.deviceConnectData.needDraw = true;
            port.deviceConnectData.odfPortStartIndex = i;
            port.deviceConnectData.odfPortEndIndex = i;
            port.device = Device.createNew(port.deviceConnectData.UNIT2_ID, port.deviceConnectData.UNIT2_DESC);
            port.device.UNIT_CLASS =  port.deviceConnectData.UNIT2_CLASS;
            prePort = port;
        }

        //增加一些方法
        chart.getTotalWidth = function(){
            return Device.width * 2 + chart.device.width + ODFConnectChart.DeviceSpace * 3;
        };
        chart.getTotalHeight = function(){
            var bodyHeight = 0;
            if(this.device.ports.length > 0){
                bodyHeight = this.device.ports.length * (PortODF.height + ODFConnectChart.PortVerSpace) - ODFConnectChart.PortVerSpace;
            }
            return Device.headerHeight + bodyHeight;
        }
        chart.draw = function(canvas){
            ODFConnectChart.draw(this, canvas);
        };
        chart.getPortByPoint = function(x ,y){
            for(var i=0;i<this.clickedObjects.length;i++){
                //1.点击的是不是此交换机接口？
                var p = this.clickedObjects[i];
                //上半部分是of_id
                if(p.x < x && p.x + p.w > x && p.y < y && p.y + p.h > y){
                    return p;
                }
            }
            return null;
        }
        return chart;
    },
    PathAscSort : function (a,b)
    {
        return a.PORT_INDEX - b.PORT_INDEX;
    },
    PortAscSort : function (a,b)
    {
        if( a.name > b.name){
            return 1;
        }
        if( a.name < b.name){
            return -1;
        }
        return 0;
    },
    PathArrAscSort:function(a,b){
        if( a[0].PORT_NAME > b[0].PORT_NAME){
            return 1;
        }
        if( a[0].PORT_NAME < b[0].PORT_NAME){
            return -1;
        }
        return 0;
    },
    draw : function(chart, canvas){
        chart.clickedObjects = [];
        //先画主装置
        var obj = chart.device;
        obj.x = chart.x + Device.width + ODFConnectChart.DeviceSpace * 3 / 2;
        obj.y = chart.y;
        //画底色
        canvas.fillStyle = Device.bodyBackground;
        var bodyHeight = 0;
        if(obj.ports.length > 0){
            bodyHeight = obj.ports.length * (PortODF.height + ODFConnectChart.PortVerSpace) - ODFConnectChart.PortVerSpace;
        }
        canvas.fillRect(obj.x, obj.y, obj.width, bodyHeight + Device.headerHeight);
        //画标题
        Device.drawTitle(obj, canvas);
        //画端口
        for(var i=0;i<obj.ports.length;i++){
            var port = obj.ports[i];
            port.x = obj.x;
            port.y = obj.y + Device.headerHeight + i * (PortODF.height + ODFConnectChart.PortVerSpace);
            PortODF.draw(port,canvas);
            chart.clickedObjects.push({
                x:port.x,
                y:port.y,
                w:PortODF.width,
                h:PortODF.height,
                TYPE:'PORT',
                data:port.data
            });
        }
        //画端口对应的装置
        for(var i=0;i<obj.ports.length;i++){
            var port = obj.ports[i];
            //查看需不需要画装置 
            if(port.deviceConnectData!=null && port.deviceConnectData.needDraw == true){
                port.device.x = port.x - Device.width - ODFConnectChart.DeviceSpace;
                port.device.y = port.y;
                var pc = port.deviceConnectData.odfPortEndIndex - port.deviceConnectData.odfPortStartIndex + 1;
                port.device.height = pc * PortODF.height + (pc - 1) * ODFConnectChart.PortVerSpace;
                Device.drawBody(port.device, canvas);
                //把此装置对应的端口都画出来
                for(var j=port.deviceConnectData.odfPortStartIndex;j<=port.deviceConnectData.odfPortEndIndex;j++){
                    var pc = j - port.deviceConnectData.odfPortStartIndex;
                    var deviceData = obj.ports[j].deviceConnectData;
                    var deviceDataNext = null;
                    if(j+1<=port.deviceConnectData.odfPortEndIndex){
                        deviceDataNext = obj.ports[j+1].deviceConnectData;
                    }
                    if(deviceDataNext!=null&&deviceDataNext.PORT2_NAME == deviceData.PORT2_NAME){
                        //连着两个端口一样，画在一起
                        canvas.fillStyle = ODFConnectChart.PortBodyBackground;
                        x = port.x - ODFConnectChart.DeviceSpace - Port.width;
                        if(pc==0){
                            y = port.device.y;
                        }else{
                            y = port.device.y + pc*PortODF.height + pc * ODFConnectChart.PortVerSpace;
                        }
                        canvas.fillRect(x,
                            y + 2, Port.width, PortODF.height *2 + ODFConnectChart.PortVerSpace - 4);
                        //画出名字
                        canvas.font = Port.fontSize + "px " + Port.fontFamily;
                        canvas.fillStyle = Port.fontColor;
                        canvas.textAlign="left";
                        canvas.fillText(deviceData.PORT2_NAME,x + 2, y + (PortODF.height *2 + ODFConnectChart.PortVerSpace + Port.fontSize) / 2 - 2 , Port.width);
                        //画接头框子
                        canvas.beginPath();
                        canvas.strokeStyle = Device.bodyBackground;
                        canvas.lineWidth= 2;
                        canvas.moveTo(x + Port.width - PortODF.height,y);
                        canvas.lineTo(x + Port.width - PortODF.height,y+PortODF.height *2 + ODFConnectChart.PortVerSpace);
                        canvas.moveTo(x + Port.width - PortODF.height, y + PortODF.height);
                        canvas.lineTo(x + Port.width, y + PortODF.height);
                        canvas.stroke();
                        //画连接线及梯口
                        ODFConnectChart.drawDeviceLiner(deviceData, obj.ports[j],chart,canvas);
                        ODFConnectChart.drawDeviceLiner(deviceDataNext, obj.ports[j+1],chart,canvas);
                        j+=1;
                    }else{
                        //单独自己一个端口
                        canvas.fillStyle = ODFConnectChart.PortBodyBackground;
                        x = port.x - ODFConnectChart.DeviceSpace - Port.width;
                        if(pc==0){
                            y = port.device.y;
                        }else{
                            y = port.device.y + pc*PortODF.height + pc * ODFConnectChart.PortVerSpace;
                        }
                        canvas.fillRect(x,
                            y + 2, Port.width, PortODF.height - 4);
                        //画出名字
                        canvas.font = Port.fontSize + "px " + Port.fontFamily;
                        canvas.fillStyle = Port.fontColor;
                        canvas.textAlign="left";
                        canvas.fillText(deviceData.PORT2_NAME,x + 2, y + (PortODF.height + Port.fontSize) / 2 - 2 , Port.width);
                        //画接头框子
                        canvas.beginPath();
                        canvas.strokeStyle = Device.bodyBackground;
                        canvas.lineWidth= 2;
                        canvas.moveTo(x + Port.width - PortODF.height,y);
                        canvas.lineTo(x + Port.width - PortODF.height,y+PortODF.height);
                        canvas.stroke();
                        ODFConnectChart.drawDeviceLiner(deviceData, obj.ports[j],chart,canvas);
                    }
                }
            }
        }
        //画端口对应的橱柜
        var cableStartPort = null;//记录
        for(var i=0;i<obj.ports.length;i++){
            var port = obj.ports[i];
            if(port.cubicleConnectData != null){
                //画连接线
                var cx = port.x + PortODF.width;
                var cy = port.y + PortODF.height / 2;
                var cw = ODFConnectChart.DeviceSpace;
                canvas.beginPath();
                canvas.strokeStyle = config.fibre_color[port.cubicleConnectData.COLOR];
                canvas.lineWidth=6;
                canvas.moveTo(cx,cy);
                canvas.lineTo(cx + cw,cy);
                canvas.stroke();
                //画光纤编号
                cx = cx + cw / 2;
                cy = cy;
                var cw = PortODF.height*3/2;
                var ch = PortODF.height / 2;
                cx -= cw/2;
                cy -= ch/2;
                canvas.fillStyle = Device.bodyBackground;
                canvas.fillRect(cx, cy, cw, ch);
                canvas.fillStyle = ODFConnectChart.PortBodyBackground;
                canvas.fillRect(cx+2, cy+2, cw-4, ch-4);
                canvas.font = Port.fontSize + "px " + Port.fontFamily;
                canvas.fillStyle = Port.fontColor;
                canvas.textAlign="center";
                canvas.fillText(port.cubicleConnectData.CONN_NAME,cx + cw/2, cy+ch/2+Port.fontSize/2-4, cw);
                chart.clickedObjects.push({
                    x:cx,
                    y:cy,
                    w:cw,
                    h:ch,
                    TYPE:'CUBICLE-FIBRE',
                    data:port.cubicleConnectData
                });
                if(cableStartPort == null){
                    cableStartPort = port;
                    cableStartPort.index = i;
                    cableStartPort.endIndex = i;
                }else{
                    if(cableStartPort.cubicleConnectData.CABLE_NAME == port.cubicleConnectData.CABLE_NAME){
                        cableStartPort.endIndex = i;
                    }else{
                        cableStartPort.endIndex = i-1;
                        ODFConnectChart.drawCubicle(cableStartPort,chart,canvas);

                        cableStartPort = port;
                        cableStartPort.index = i;
                        cableStartPort.endIndex = i;
                    }
                }
            }else if(cableStartPort != null){
                cableStartPort.endIndex = i-1;
                ODFConnectChart.drawCubicle(cableStartPort,chart,canvas);
                cableStartPort = null;
            }
        }
        if(cableStartPort!=null){
            ODFConnectChart.drawCubicle(cableStartPort,chart,canvas);
        }
    },
    drawDeviceLiner : function(deviceData, odfPort, chart, canvas){
        //画对应的装置接口
        var x = odfPort.x - ODFConnectChart.DeviceSpace - PortODF.height + 2;
        var y = odfPort.y + 4;
        var w = PortODF.height - 4;
        var h = PortODF.height - 8;
        var interfaceBackground = '#609bc3';//突出部颜色
        var bgImg = new Image();
        bgImg.src = config.icon_right_out_url;
        if(deviceData.PORT2_DIRECTION == "Rx"){
            bgImg.src = config.icon_right_in_url;
        }
        if(bgImg.complete){
            canvas.drawImage(bgImg,x, y,w,h);
        }else{
            bgImg.onload = function(){
                canvas.drawImage(bgImg,x, y,w,h);
            }
        }
        chart.clickedObjects.push({
            x:x,
            y:y,
            w:w,
            h:h,
            TYPE:'PORT',
            data:deviceData
        });
        //画连接线
        canvas.beginPath();
        canvas.strokeStyle = config.fibre_color[0];
        canvas.lineWidth=6;
        canvas.moveTo(x+w+4,y+h/2);
        canvas.lineTo(odfPort.x,y+h/2);
        canvas.stroke();
        //画光纤编号
        var cx = odfPort.x - (odfPort.x - x - PortODF.height) / 2;
        var cy = odfPort.y + PortODF.height/2;
        var cw = PortODF.height*3/2;
        var ch = PortODF.height / 2;
        cx -= cw/2;
        cy -= ch/2;
        canvas.fillStyle = Device.bodyBackground;
        canvas.fillRect(cx, cy, cw, ch);
        canvas.fillStyle = ODFConnectChart.PortBodyBackground;
        canvas.fillRect(cx+2, cy+2, cw-4, ch-4);
        canvas.font = Port.fontSize + "px " + Port.fontFamily;
        canvas.fillStyle = Port.fontColor;
        canvas.textAlign="center";
        canvas.fillText(deviceData.CONN_NAME,cx + cw/2, cy+ch/2+Port.fontSize/2-4, cw);
        chart.clickedObjects.push({
            x:cx,
            y:cy,
            w:cw,
            h:ch,
            TYPE:'DEVICE-FIBRE',
            data:deviceData
        });
    },
    drawCubicle : function(cablePort, chart, canvas){
        var device = chart.device;
        var topPort = device.ports[cablePort.index];
        var bottomPort = device.ports[cablePort.endIndex]
        //画竖线
        canvas.beginPath();
        canvas.strokeStyle = config.cable_color;
        canvas.lineWidth=6;
        canvas.moveTo(topPort.x + device.width + ODFConnectChart.DeviceSpace, 
            topPort.y + PortODF.height / 2 - 3);
        canvas.lineTo(bottomPort.x + device.width + ODFConnectChart.DeviceSpace, 
            bottomPort.y + PortODF.height / 2 + 3);
        canvas.stroke();
        //画横向电缆
        canvas.beginPath();
        canvas.strokeStyle = config.cable_color;
        canvas.lineWidth=6;
        canvas.moveTo(topPort.x + device.width + ODFConnectChart.DeviceSpace, 
            topPort.y + (bottomPort.y - topPort.y) /2 + PortODF.height / 2);
        canvas.lineTo(bottomPort.x + device.width + ODFConnectChart.DeviceSpace * 2 , 
            topPort.y + (bottomPort.y - topPort.y) /2 + PortODF.height / 2);
        canvas.stroke();
        //画电缆名称
        cx = topPort.x + device.width + ODFConnectChart.DeviceSpace + ODFConnectChart.DeviceSpace/2;
        cy = topPort.y + (bottomPort.y - topPort.y) /2 + PortODF.height / 2;
        var cw = PortODF.height*3/2 + 10;
        var ch = PortODF.height / 2;
        cx -= cw/2;
        cy -= ch/2;
        canvas.fillStyle = Device.bodyBackground;
        canvas.fillRect(cx, cy, cw, ch);
        canvas.fillStyle = ODFConnectChart.PortBodyBackground;
        canvas.fillRect(cx+2, cy+2, cw-4, ch-4);
        canvas.font = Port.fontSize + "px " + Port.fontFamily;
        canvas.fillStyle = Port.fontColor;
        canvas.textAlign="center";
        canvas.fillText(cablePort.cubicleConnectData.CABLE_NAME,cx + cw/2, cy+ch/2+Port.fontSize/2-4, cw);
        chart.clickedObjects.push({
            x:cx,
            y:cy,
            w:cw,
            h:ch,
            TYPE:'CUBICLE-CABLE',
            data:cablePort.cubicleConnectData
        });
        //画电缆对应的橱柜
        var cubicle = Device.createNew(cablePort.cubicleConnectData.CUBICLE2_ID,cablePort.cubicleConnectData.CUBICLE2_DESC);
        cubicle.UNIT_CLASS = 'CABINET';
        cubicle.height = 100;
        cubicle.x = topPort.x + device.width + ODFConnectChart.DeviceSpace + ODFConnectChart.DeviceSpace;
        cubicle.y = topPort.y + (bottomPort.y - topPort.y) /2 + PortODF.height / 2 - cubicle.height / 2;
        cubicle.width = Device.width - Port.width;
        Device.drawBody(cubicle, canvas);
    },
};